Weekday Market Patterns

Author

Holmdex

Published

May 3, 2025

# Add this to the beginning of your load-market-data chunk

# Create a data directory if it doesn't exist
if (!dir.exists("data")) {
  dir.create("data", showWarnings = FALSE)
}

# Define constants for data processing
MAX_RETURN_VALUE <- 20  # Cap at ยฑ20% for daily returns as a reasonable bound
MAX_REGIONAL_RETURN <- 5  # Cap at ยฑ5% for regional averages
WINSORIZE_PERCENTILE <- 0.005  # Winsorize at 0.5% on each tail

# Create a robust cache file path system for storing data
cache_file <- file.path("data", "global_market_data_cache_5yr.rds")
cache_file_weekly <- file.path("data", paste0("global_market_data_cache_5yr_", format(Sys.Date(), "%Y_%W"), ".rds"))
cache_file_daily <- file.path("data", paste0("global_market_data_cache_5yr_", format(Sys.Date(), "%Y_%m_%d"), ".rds"))

# Function to safely read cache files
safe_read_cache <- function(file_path) {
  tryCatch({
    if (file.exists(file_path)) {
      message(paste("Attempting to load cache from:", file_path))
      data <- readRDS(file_path)
      
      # Basic validation
      if (is.data.frame(data) && nrow(data) > 0 && 
          all(c("date", "symbol", "close") %in% colnames(data))) {
        message(paste("Successfully loaded cache with", nrow(data), "rows from", file_path))
        return(data)
      } else {
        message("Cache data structure is invalid or empty")
      }
    }
    return(NULL)
  }, error = function(e) {
    message(paste("Error reading cache file:", e$message))
    return(NULL)
  })
}

# Function to safely save cache
safe_save_cache <- function(data, file_path) {
  tryCatch({
    # Create directory if needed
    dir.create(dirname(file_path), showWarnings = FALSE, recursive = TRUE)
    
    # Save data
    saveRDS(data, file_path)
    message(paste("Successfully saved cache to:", file_path))
    return(TRUE)
  }, error = function(e) {
    message(paste("Failed to save cache to:", file_path, "-", e$message))
    return(FALSE)
  })
}

market_data <- NULL

# Try to load from cache files in order of recency
if(is.null(market_data)) {
  market_data <- safe_read_cache(cache_file_daily)
}

if(is.null(market_data)) {
  market_data <- safe_read_cache(cache_file_weekly)
}

if(is.null(market_data)) {
  market_data <- safe_read_cache(cache_file)
}

# If no cache, new code for fetching data will run as defined in your existing logic
# A tibble: 10 ร— 2
   region          count
   <chr>           <int>
 1 Western Europe      7
 2 East Asia           5
 3 Middle East         5
 4 North America       5
 5 South Asia          5
 6 Africa              4
 7 Eastern Europe      4
 8 Latin America       4
 9 Northern Europe     3
10 Oceania             2

Global Patterns: Is There a โ€œBest Dayโ€ for Markets?

The chart below shows average returns by weekday across all global markets. Green bars show better performance, while red bars show weaker days.

Markets included in this analysis: ๐Ÿ‡ฆ๐Ÿ‡ช Abu Dhabi, ๐Ÿ‡ฆ๐Ÿ‡ท MERVAL Argentina, ๐Ÿ‡ฆ๐Ÿ‡บ S&P/ASX 200, ๐Ÿ‡ง๐Ÿ‡ท Bovespa, ๐Ÿ‡จ๐Ÿ‡ฆ S&P/TSX Composite, ๐Ÿ‡จ๐Ÿ‡ญ Swiss Market Index, ๐Ÿ‡จ๐Ÿ‡ฑ S&P CLX IPSA, ๐Ÿ‡จ๐Ÿ‡ณ CSI 300, ๐Ÿ‡ฉ๐Ÿ‡ช DAX 40, ๐Ÿ‡ฉ๐Ÿ‡ฐ OMX Copenhagen, ๐Ÿ‡ช๐Ÿ‡ฌ Egypt EGX 30, ๐Ÿ‡ช๐Ÿ‡ธ IBEX 35, ๐Ÿ‡ช๐Ÿ‡บ EURO STOXX 50, ๐Ÿ‡ซ๐Ÿ‡ท CAC 40, ๐Ÿ‡ฌ๐Ÿ‡ง FTSE 100, ๐Ÿ‡ญ๐Ÿ‡ฐ Hang Seng, ๐Ÿ‡ฎ๐Ÿ‡ฉ Jakarta Composite, ๐Ÿ‡ฎ๐Ÿ‡ณ BSE SENSEX, ๐Ÿ‡ฎ๐Ÿ‡ณ Nifty 50, ๐Ÿ‡ฎ๐Ÿ‡น FTSE MIB, ๐Ÿ‡ฏ๐Ÿ‡ต Nikkei 225, ๐Ÿ‡ฐ๐Ÿ‡ท KOSPI, ๐Ÿ‡ฒ๐Ÿ‡ฝ IPC Mexico, ๐Ÿ‡ฒ๐Ÿ‡พ Malaysia KLCI, ๐Ÿ‡ณ๐Ÿ‡ฌ Nigeria SE, ๐Ÿ‡ณ๐Ÿ‡ด Oslo Exchange, ๐Ÿ‡ต๐Ÿ‡ฑ Poland WIG, ๐Ÿ‡ท๐Ÿ‡บ MOEX Russia, ๐Ÿ‡ธ๐Ÿ‡ช OMX Stockholm, ๐Ÿ‡ธ๐Ÿ‡ฌ Straits Times, ๐Ÿ‡น๐Ÿ‡ท BIST 30, ๐Ÿ‡น๐Ÿ‡ผ Taiwan Weighted, ๐Ÿ‡บ๐Ÿ‡ธ Dow Jones, ๐Ÿ‡บ๐Ÿ‡ธ Nasdaq, ๐Ÿ‡บ๐Ÿ‡ธ Russell 2000, ๐Ÿ‡บ๐Ÿ‡ธ S&P 500, ๐Ÿ‡ฟ๐Ÿ‡ฆ JSE Top 40

Regional Comparison: Do Weekday Effects Vary by Region?

Different regions around the world show different weekday patterns, which may be influenced by local trading behavior, timezone differences, and market structure.

Check Your Marketโ€™s Pattern

Different markets can show different patterns. Use the navigation arrows to explore weekday performance for each market:

Best and Worst Days by Region

The table below shows the best and worst performing weekdays for each region: